﻿Public Class clsTrStatystyki

#Region "Attributes"

    Private mvarDrut As Collection(Of clsInhTrDrut)
    Private mvarDrutWyslany As Collection(Of clsInhTrDrut)

#End Region

#Region "Properties"

    Public Property Drut() As Collection(Of clsInhTrDrut)
        Get
            Return mvarDrut
        End Get
        Set(ByVal value As Collection(Of clsInhTrDrut))
            mvarDrut = value
        End Set
    End Property

    Public Property DrutWyslany() As Collection(Of clsInhTrDrut)
        Get
            Return mvarDrutWyslany
        End Get
        Set(ByVal value As Collection(Of clsInhTrDrut))
            mvarDrutWyslany = value
        End Set
    End Property

#End Region

#Region "Methods"

    Public Sub New()
        mvarDrutWyslany = New Collection(Of clsInhTrDrut)
        mvarDrut = New Collection(Of clsInhTrDrut)
    End Sub

    Public Sub WyslanyDrut(DbHolder As clsDataBase, Zapytanie As String)

        Dim lvarZapytanie As String
        lvarZapytanie = Zapytanie

        Dim lvarReader As OdbcDataReader = Nothing
        If DbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(DbHolder, "queries_application_tracking", "cls_statystyki", 1, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_zapytanie", Zapytanie)
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                While lvarReader.Read
                    mvarDrutWyslany.Add(New clsInhTrDrut(CInt(lvarReader.Item("drut_id")), DbHolder))
                End While
            End If
        End If

    End Sub

    Public Sub ZrobionyDrut(DbHolder As clsDataBase, Zapytanie As String)

        Dim lvarZapytanie As String
        lvarZapytanie = Zapytanie

        Dim lvarReader As OdbcDataReader = Nothing
        If DbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(DbHolder, "queries_application_tracking", "cls_statystyki", 2, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_zapytanie", Zapytanie)
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                While lvarReader.Read
                    mvarDrut.Add(New clsInhTrDrut(CInt(lvarReader.Item("id")), DbHolder))
                End While
            End If











        End If
    End Sub





    '    If lvarReader.HasRows Then
    '    lvarNewRow = mvarDrutTab.NewRow()
    '    With lvarNewRow
    '        .Item("drut_id") = IIf(IsDBNull(lvarReader.Item("id")), "", lvarReader.Item("id"))
    '        .Item("numer") = IIf(IsDBNull(lvarReader.Item("numer")), "", lvarReader.Item("numer"))
    '        .Item("klient_nazwa") = IIf(IsDBNull(lvarReader.Item("nazwa_skrocona")), "", lvarReader.Item("nazwa_skrocona"))
    '          End With
    '    mvarDrutTab.Rows.Add(lvarNewRow)
    'End If





    Shared Function ObliczIloscOperacji(DrutId As Integer, DbHolder As clsDataBase) As Double

        Dim lvarWyznMaterialuProdukcyjnego As Boolean = False
        Dim lvarWyznIlosciDrutRodzic As Boolean = False
        Dim lvarDrut1 As clsInhTrDrut
        Dim lvarDrut2 As clsInhTrDrut
        lvarDrut1 = New clsInhTrDrut(DrutId, DbHolder)
        Dim lvarLicznik As Integer = 0
        Dim lvarTabela As New DataTable
        Dim lvarNewRow As DataRow

        Dim lvarTabList As List(Of DataTable)
        lvarTabList = New List(Of DataTable)

        With lvarTabela.Columns
            .Add("pracownik", GetType(Integer))
            .Add("wartosc", GetType(Double))
        End With

        Do
            For Each lvarTmp As clsTrRodzic In lvarDrut1.Rodzic
                If lvarDrut1.Rodzic.Count > 1 Then
                    If Not lvarTmp.WyznMaterialProdukcyjny Then
                        '   lvarDrut2 = New clsInhTrDrut(lvarTmp.DrutRodzicId, DbHolder)
                        Dim lvarTab As New DataTable

                        With lvarTab.Columns
                            .Add("drut_id", GetType(Integer))
                            .Add("ilosc_operacji", GetType(Integer))
                        End With
                        lvarNewRow = lvarTab.NewRow()
                        With lvarNewRow
                            .Item("drut_id") = lvarTmp.DrutRodzicId
                            .Item("ilosc_operacji") = lvarLicznik
                        End With
                        lvarTab.Rows.Add(lvarNewRow)
                        lvarTab.TableName = lvarTab.TableName & "_" & lvarTmp.DrutRodzicId
                        lvarTabList.Add(lvarTab)
                    Else
                        lvarWyznMaterialuProdukcyjnego = True
                    End If
                Else
                    If Not lvarTmp.WyznMaterialProdukcyjny Then
                        lvarLicznik = lvarLicznik + 1
                        lvarNewRow = lvarTabela.NewRow()
                        With lvarNewRow
                            .Item("pracownik") = lvarDrut1.PracownikId
                            .Item("wartosc") = 0
                        End With
                        lvarTabela.Rows.Add(lvarNewRow)
                        lvarDrut1 = New clsInhTrDrut(lvarTmp.DrutRodzicId, DbHolder)
                    Else
                        lvarWyznMaterialuProdukcyjnego = True
                        ' mvarMaterialProdukcyjny = New clsInhTrMaterialProdukcyjny(lvarTmp.DrutRodzicId, DbHolder)
                    End If
                End If
            Next
        Loop Until (lvarWyznMaterialuProdukcyjnego Or lvarWyznIlosciDrutRodzic)

        'obliczenie ilosci drutu
        Dim lvarWaga As Double
        lvarDrut1 = New clsInhTrDrut(DrutId, DbHolder)
        For Each lvarRodzic As clsTrRodzic In lvarDrut1.Rodzic
            lvarWaga = lvarWaga + CDbl(lvarRodzic.Waga)
        Next
        If lvarTabList.Count = 0 Then
            Do
                For Each lvarTmp As clsTrRodzic In lvarDrut1.Rodzic
                    If Not lvarTmp.WyznMaterialProdukcyjny Then
                        For Each lvarRow As DataRow In lvarTabela.Rows
                            If CInt(lvarRow.Item("pracownik")) = lvarDrut1.PracownikId Then
                                lvarRow.Item("wartosc") = CDbl(lvarRow.Item("wartosc")) + (lvarWaga / lvarLicznik)
                            End If
                        Next
                        lvarDrut1 = New clsInhTrDrut(lvarTmp.DrutRodzicId, DbHolder)
                    Else
                        lvarWyznMaterialuProdukcyjnego = True
                    End If
                Next
            Loop Until (lvarWyznMaterialuProdukcyjnego)

        End If

        For Each lvarRow As DataRow In lvarTabela.Rows
            MessageBox.Show(lvarRow.Item("pracownik").ToString() & "    " & lvarRow.Item("wartosc").ToString())
        Next

        Return lvarWaga

    End Function

#End Region

End Class
